﻿package Classes.GameUtils
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;
	
	/**
	 * 自定义显示的提示类
	 * 使用方式 
	 * 给mc加一个提示，提示的方向在mc的上面
	 * ToolTip_Sprite.getInstance().setTip(mc,new MyTipSprite(data),ToolTip_Sprite.UP);//MyTipSprite 继承自 BaseTipSprite 并重写了内部方法
	 * @author 翼翔天外
	 * @e-mail onechenxing@yahoo.com.cn
	 */
	public class ToolTip_Sprite
	{
		/**
		 * 提示显示方向 - 左 
		 */
		public static const DIRECTION_LEFT:int 		= 1;
		/**
		 * 提示显示方向 - 右 
		 */
		public static const DIRECTION_RIGHT:int 		= 2;
		/**
		 * 提示显示方向 - 上 
		 */
		public static const DIRECTION_UP:int 			= 3;
		/**
		 * 提示显示方向 - 下
		 */
		public static const DIRECTION_DOWN:int 		= 4;
		
		/**
		 * 默认ToolTip的最小宽度 
		 */
		public static const DEFAULT_MIN_WIDTH:int 	= 50;
		/**
		 * 默认ToolTip的最小高度 
		 */
		public static const DEFAULT_MIN_HEIGHT:int 	= 30;
		/**
		 * 默认的背景框与ui的边距宽度
		 */
		public static const DEFAULT_MARGIN_WIDTH:int 	= 3;
		/**
		 * 默认的背景框与ui的边距高度
		 */
		public static const DEFAULT_MARGIN_HEIGHT:int = 3;
		
		//单例对象
		private static var _instance:ToolTip_Sprite;
		
		/**
		 * 提示字典 
		 */
		private var _table:Dictionary = new Dictionary(true);	
		
		/**
		 * 需要动态改变提示的字典 
		 */		
		private var _tableTime:Dictionary=new Dictionary(true);
		
		public function ToolTip_Sprite(param:Singleton)
		{
			
		}
		
		/**
		 * 获取单例对象 
		 * @return 
		 * 
		 */
		public static function getInstance():ToolTip_Sprite
		{
			if(ToolTip_Sprite._instance==null)
			{
				ToolTip_Sprite._instance = new ToolTip_Sprite(new Singleton);
			}
			return ToolTip_Sprite._instance;
		}
		
		/**
		* 设置提示 
		 * @param target	目标的显示对象
		 * @param tipSprite	提示的显示对象
		 * @param direction 提示的默认反向
		 * @param isChangeTips 文本提示需要随时改变   默认为false(不需要)
		 * @param offx  偏移量 沿 X轴
		 * @param offy  偏移量 沿 Y轴
		 */		
		public function setTip(target:DisplayObject,tipSprite:BaseTipSprite,direction:int = DIRECTION_UP,isChangeTips:Boolean=false,offx:int=0,offy:int=0):void
		{
			_table[target] = new TipVO(tipSprite,direction,offx,offy);
			
			if(isChangeTips)
			{
				_tableTime[target] = _table[target];
			}
			target.addEventListener(MouseEvent.ROLL_OVER,onRollOver,false,0,true);		
			target.addEventListener(MouseEvent.ROLL_OUT,onRollOut,false,0,true);
			//解决点击后的弹出层，tooltip会不消失问题
			target.addEventListener(MouseEvent.CLICK, onRollOut, false, 0, true);
//			target.addEventListener(Event.REMOVED_FROM_STAGE,onRollOut,false,0,true);
			
//			target.addEventListener(Event.ADDED_TO_STAGE,onRollOver,false,0,true);
		}
		
		
		/**
		 * 重新设置热感文本 
		 * @param txt
		 * @param target
		 * @param isTitle  是否是设置标题
		 */			
		public function setTipTxt(txt:String,target:DisplayObject,isTitle:Boolean=false):void
		{
			var tipVo:TipVO = _tableTime[target];
			if(null==tipVo)return;
			
			if(isTitle){
				tipVo.tipSprite.setTitleText(txt);
			}
			else
			tipVo.tipSprite.setTextFieldTxt(txt);
		}
		
		/**
		 * 手动移除提示 
		 * @param target
		 * 
		 */
		public function removeTip(target:DisplayObject):void
		{
			target.dispatchEvent(new MouseEvent(MouseEvent.ROLL_OUT));
			if(null!=this._table[target]) delete this._table[target];
			if(null!=this._tableTime[target]) delete this._tableTime[target];
			target.removeEventListener(MouseEvent.ROLL_OVER,onRollOver,false);		
			target.removeEventListener(MouseEvent.ROLL_OUT,onRollOut,false);
			target.removeEventListener(MouseEvent.CLICK, onRollOut,false);
			target.removeEventListener(Event.REMOVED_FROM_STAGE,onRemoveStage,false);
		}
		
		private function onRollOver(e:Event):void
		{
			var target:DisplayObject = e.currentTarget as DisplayObject;
			var container:DisplayObjectContainer = target.stage as DisplayObjectContainer;
			removeOldTip(container);
			var tipVo:TipVO = _table[target];
			var tip:Tip = new Tip(tipVo.tipSprite);
			container.addChild(tip);
			tip.name = "tip";
			tip.direction = tipVo.direction;
			tip.targetBounds = target.getBounds(container);
			tip.offx=tipVo.offx;
			tip.offy=tipVo.offy;
			tip.updateSize();	
		}
		
		private function onRemoveStage(e:Event):void
		{
			var target:DisplayObject = e.currentTarget as DisplayObject;
			var container:DisplayObjectContainer = target.stage as DisplayObjectContainer;
			removeOldTip(container);
			if(null!=this._table[target]) delete this._table[target];
			if(null!=this._tableTime[target]) delete this._tableTime[target];
		}
		
		private function onRollOut(e:Event):void
		{
			var target:DisplayObject = e.currentTarget as DisplayObject;
			var container:DisplayObjectContainer = target.stage as DisplayObjectContainer;
			removeOldTip(container);
		}
		
		private function removeOldTip(container:DisplayObjectContainer):void
		{
			if(container == null) return;
			var tip:DisplayObject = container.getChildByName("tip");
			if(tip)	container.removeChild(tip);
		}


	}
}
import Classes.GameUtils.BaseTipSprite;


class TipVO
{
	public var tipSprite:BaseTipSprite;
	public var direction:int;
	public var offx:int;
	public var offy:int;
	public function TipVO(tipSprite:BaseTipSprite,direction:int,offx:int=0,offy:int=0)
	{
		this.tipSprite = tipSprite;
		this.direction = direction;
		this.offx=offx;
		this.offy=offy;
	}
}
class Singleton{}